package net.akkz.action.student;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.akkz.beanFactory.BeanFactory;
import net.akkz.model.Student;
import net.akkz.server.StudentServer;

@WebServlet(name="StudentLogin", urlPatterns="/student/studentLogin.do")
public class StudentLogin extends HttpServlet
{
	private StudentServer studentServer;
	
	@Override
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException,ServletException
	{
		String password = request.getParameter("password");
		String name = request.getParameter("id");
		
		Student student = new Student();
		student.setPassword(password);
		student.setName(name);
		
		student = studentServer.login(student);
		
		if(student == null)
		{
			request.setAttribute("state", "error");
		}
		else if(student.getId() == 0)
		{
			request.setAttribute("state", student.getName());
		}
		else
		{
			request.getSession().setAttribute("student", student);
			
			request.setAttribute("state", "success");
		}
		
		request.getRequestDispatcher("/jsp/state.jsp").forward(request, response);
	}
	
	@Override
	public void init()
	{
		BeanFactory  bf = (BeanFactory) this.getServletContext().getAttribute("beanFactory");
		
		studentServer = bf.getBean("studentServer");
	}
}
